﻿/**
 * Created in 2012-2-7.
 */
package kit4j.tests.jdk.awt;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import kit4j.generator.keyless.IKeylessGenerator;
import kit4j.generator.keyless.RandomChineseFontKeylessGenerator;
import kit4j.generator.keyless.RandomChineseStringKeylessGenerator;

/**
 * 
 * @author MiniKnife
 * 
 */
public class TestFontDisplay {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		final IKeylessGenerator<String> stringGenerator = new RandomChineseStringKeylessGenerator(6);
//		final IGenerator<String, Font> g = new RandomFontGenerator() {
//			@Override
//			public Font next(String str) {
//				Font[] fonts = getCanDisplayFonts(str);
//
//				if (fonts.length == 0) {
//					return null;
//				} else {
//					return fonts[fontCount++ % fonts.length];
//				}
//			}
//		};
		IKeylessGenerator<Font> fontGenerator = new RandomChineseFontKeylessGenerator();
		
		int testCount = 1000;

		testFontDisplayEfficiency(fontGenerator, stringGenerator, testCount);
	}

	private static void testFontDisplayEfficiency(
			IKeylessGenerator<Font> fontGenerator, IKeylessGenerator<String> stringGenerator,
			int testCount) {
		Graphics g = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB)
				.getGraphics();
		long time, timeSum = 0;
		long minTime = Long.MAX_VALUE, maxTime = Long.MIN_VALUE;
		Font minTimeFont = null, maxTimeFont = null;
		Font font;

		for (int i = 0; i < testCount; ++i) {
			font = fontGenerator.next().deriveFont(48F);
			time = System.currentTimeMillis();

			g.setFont(font);
			g.drawString(stringGenerator.next(), 0, 150);

			time = System.currentTimeMillis() - time;
			timeSum += time;

			if (time < minTime) {
				minTime = time;
				minTimeFont = font;
			} else if (time > maxTime) {
				maxTime = time;
				maxTimeFont = font;
			}
		}

		System.out.println("fontGenerator:" + fontGenerator + ",stringGenerator:"
				+ stringGenerator + ",testCount:" + testCount + ",time:" + timeSum);
		System.out.println("minTime:" + minTime + ",font:" + minTimeFont);
		System.out.println("maxTime:" + maxTime + ",font:" + maxTimeFont);
	}

}
